//    Copyright (C) 2010 by Dolphin Technoly
//    All rights reserved.
// 
//    Copyright Notification
//    No part may be reproduced except as authorized by written permission
// 
//    @file   dti_status_decode.v
//    @par    Company:
//              Dolphin Technology
//    @par    Project:
//              [projectname]
//    @par    Author:
//              N. Huy Bui
//    @par    Date:
//              July 17, 2012
//    @brieft
//              status decoder
//$Id$

module dti_status_decode (pop_clk, pop_rst_n, pop_req_n, fifo_empty, part_wd, pop_error);

parameter 	  ERR_MODE    = 1; //dynamic flag
parameter 	  IN_WIDTH    = 24;
parameter 	  OUT_WIDTH   = 8;
parameter 	  BYTE_ORDER  = 1;

input 		  pop_clk;
input 		  pop_rst_n;
input 		  pop_req_n; 			//pop request, active low 
input 		  fifo_empty;
output reg 	  part_wd; 
output reg 	  pop_error;
reg 		  part_wd_next;
reg 		  pop_error_next;

wire 	  [1:0]   count_out;

dti_counter #(IN_WIDTH, OUT_WIDTH, BYTE_ORDER)
  dti_counter_inst (pop_clk, pop_rst_n, pop_req_n, fifo_empty, count_out);

always @ (posedge pop_clk or pop_rst_n)
begin
  if (!pop_rst_n) begin
    part_wd <= 0;
    pop_error <= 0;
  end
  else begin
    part_wd <= part_wd_next;
    pop_error <= pop_error_next;
  end
end

generate 
  if (ERR_MODE) begin //dynamic flag
    always @ (pop_req_n, fifo_empty, count_out, pop_error)
    begin
      if ((pop_req_n == 0) && (fifo_empty == 1)) begin
	pop_error_next = 1;
	part_wd_next = 0;
      end
      else if (fifo_empty) begin
	pop_error_next = 0;
	part_wd_next = 0;
      end
      else if (!pop_req_n) begin
	part_wd_next = (count_out == 2) ? 0 : 1;
	pop_error_next = 0;
      end
      else begin
	part_wd_next = part_wd;
	pop_error_next = 0;
      end
    end
end
else begin //sticky flag
    always @ (pop_req_n, fifo_empty, count_out, pop_error)
    begin
      pop_error_next = 0;
      if ((pop_req_n == 0) && (fifo_empty == 1)) begin
	pop_error_next = 1;
	part_wd_next = 0;
      end
      else if (fifo_empty) begin
	part_wd_next = 0;
	pop_error_next = pop_error;
      end
      else if (!pop_req_n) begin
	part_wd_next = (count_out == 2) ? 0 : 1;
	pop_error_next = pop_error;
      end
      else begin
	part_wd_next = part_wd;
	pop_error_next = pop_error;
      end
    end
end
endgenerate

endmodule // dti_status_decode
